package com.woniuxy.controller;


import com.woniuxy.controller.form.DeleteGoodsForm;
import com.woniuxy.controller.form.FindGoodsLikeForm;
import com.woniuxy.controller.form.GoodsAddForm;
import com.woniuxy.controller.form.PageAllGoodsForm;
import com.woniuxy.model.PawnGoods;
import com.woniuxy.service.PawnGoodsService;
import com.woniuxy.utils.Result;
import com.woniuxy.utils.commons.Acc;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/pawnGoods")
@Api(tags = "商品接口")
public class PawnGoodsController {

    @Resource
    private PawnGoodsService pawnGoodsService;

    /**
     * 添加商品
     * @param files
     * @param goodsAddForm
     * @return
     * @throws IOException
     */
    @PostMapping("/addGoods")
    @ResponseBody
    @ApiOperation("添加商品")
    public Result addGoods(@RequestParam("file")MultipartFile[] files,GoodsAddForm goodsAddForm) throws IOException {
        Result result = pawnGoodsService.addGoods(files, goodsAddForm);
        System.out.println(goodsAddForm);
        return result;
    }

    /**
     * 获取全部商品
     * @param pageAllGoodsform
     * @return
     */
    @Acc
    @ApiOperation("获取全部商品")
    @GetMapping("/findgoods")
    public Result getAllGoodspage(PageAllGoodsForm pageAllGoodsform){

        return pawnGoodsService.findGoodspage(pageAllGoodsform);
    }


    /**
     * 更新商品（带文件）
     * @param files
     * @param goodsAddForm
     * @return
     * @throws IOException
     */
    @PostMapping("/updateGoods")
    @ResponseBody
    @ApiOperation("更新商品（带文件）")
    public Result updateGoods(@RequestParam("file")MultipartFile[] files,GoodsAddForm goodsAddForm) throws IOException {
        System.out.println(goodsAddForm);
        Result result = pawnGoodsService.updateGoods(files, goodsAddForm);
        return result;
    }

    /**
     * 更新商品（不带文件）
     * @param goodsAddForm
     * @return
     */
    @PostMapping("/updateGoodsNoFile")
    @ResponseBody
    @ApiOperation("更新商品（不带文件）")
    public Result updateGoodsNoFile(GoodsAddForm goodsAddForm){
        System.out.println(goodsAddForm);
        Result result = pawnGoodsService.updateGoodsNoFile(goodsAddForm);
        return result;
    }


    /**
     * 根据id查询商品
     * @param goodsId
     * @return
     */
    @GetMapping("/findGoodsById")
    @ResponseBody
    @ApiOperation("根据id查询商品")
    public Result findGoodsById(Integer goodsId){
        PawnGoods goods = pawnGoodsService.getById(goodsId);
        return Result.success(200,"findGoodsById成功",goods);
    }

    /**
     * 模糊查询商品
     * @param findGoodsLikeForm
     * @return
     */
    @GetMapping("/findGoodsLike")
    @ResponseBody
    @ApiOperation("模糊查询商品")
    public Result findGoodsLike(FindGoodsLikeForm findGoodsLikeForm){
        Result result = pawnGoodsService.findGoodsLike(findGoodsLikeForm);
        return result;
    }

    /**
     * 删除商品
     * @param deleteGoodsForm
     * @return
     */
    @RequestMapping("/deleteGoods")
    @ResponseBody
    @ApiOperation("删除商品")
    public Result deleteGoods(DeleteGoodsForm deleteGoodsForm){
        System.out.println(deleteGoodsForm);
        List<String> ids = deleteGoodsForm.getIds();
        System.out.println(ids);
        for(String id : ids){
            pawnGoodsService.removeById(id);
        }
        return Result.success(200,"删除成功",null);
    }
}

